package com.dyz.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dyz.domain.SysPrivilege;
import com.dyz.model.Result;
import com.dyz.service.ISysPrivilegeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

@RestController
@Api(tags = "权限管理")
@RequestMapping("/privileges")
public class SysPrivilegesController {
    @Resource
    private ISysPrivilegeService sysPrivilegeService;
    @GetMapping
    @ApiOperation(value ="查询权限配置列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页显示条数", dataType = "Integer", paramType = "query")
    })
    @PreAuthorize("hasAuthority('sys_privilege_query')")
    public Result<Page<SysPrivilege>> getAllPrivileges(@ApiIgnore Page<SysPrivilege> page){
        page.addOrder(OrderItem.desc("last_update_time"));
     return Result.success(sysPrivilegeService.page(page));
    }
    @PostMapping
    @ApiOperation(value = "新增权限配置")
    @PreAuthorize("hasAuthority('sys_privilege_create')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysPrivilege", value = "sysPrivilege的json数据"),
    })
    public Result<?> addPrivilege(@RequestBody @Validated SysPrivilege sysPrivilege){
        boolean save = sysPrivilegeService.save(sysPrivilege);
        if(save) {
            return Result.success("新增成功");
        }
        return Result.fail("新增失败");
    }
    @PatchMapping
    @ApiOperation(value = "修改权限配置")
    @PreAuthorize("hasAuthority('sys_privilege_update')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysPrivilege", value = "sysPrivilege的json数据"),
    })
    public Result<?> updatePrivilege(@RequestBody @Validated SysPrivilege sysPrivilege){
        boolean update = sysPrivilegeService.updateById(sysPrivilege);
        if(update) {
            return Result.success("修改成功");
        }
        return Result.fail("修改失败");
    }
}

